package com.jtx.miaoah2.Cache;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jtx.miaoah2.Common.RedisConstants;
import com.jtx.miaoah2.Model.goods.DO.SkGoodsSeckill;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author 季台星
 * @Date 2023 07 23 17 26
 */
@Component
public class GoodsSeckillCache {
    @Autowired
    private StringRedisTemplate redisTemplate;

    public  boolean addSeckill(SkGoodsSeckill skGoodsSeckill){
        try {
            String jsonString = JSON.toJSONString(skGoodsSeckill);
            redisTemplate.opsForValue().set(RedisConstants.SECKILL_INFO + skGoodsSeckill.getId()
                    , jsonString, 30 * 60 * 1000 + RedisConstants.getRandomNum(), TimeUnit.MILLISECONDS);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    public  SkGoodsSeckill getSeckill(Long id){
        String seckillJSON = redisTemplate.opsForValue().get(RedisConstants.SECKILL_INFO + id);
        if (Strings.isBlank(seckillJSON)){
            return null;
        }
        return JSON.parseObject(seckillJSON, SkGoodsSeckill.class);
    }

    public  boolean addSeckillList(IPage<SkGoodsSeckill> skGoodsSeckills,Integer pageNo,Integer pageSize){
        try {
            String jsonString = JSON.toJSONString(skGoodsSeckills);
            /**
             * 防止缓存雪崩
             */
            redisTemplate.opsForValue().set(RedisConstants.SECKILL_LIST_INFO + pageNo + "_" + pageSize
                    , jsonString, 10 * 1000 + RedisConstants.getRandomNum(), TimeUnit.MILLISECONDS);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    public IPage<SkGoodsSeckill> getSeckillList(Integer pageNo, Integer pageSize){
        String seckillJSON = redisTemplate.opsForValue().get(RedisConstants.SECKILL_LIST_INFO + pageNo + "_" + pageSize);
        if (Strings.isBlank(seckillJSON)){
            return null;
        }
        return JSON.parseObject(seckillJSON, IPage.class);
    }

    /**
     * 创建商品数量，每次预下单从这里库存-1
     * key 为 秒杀场次id + 商品 id达到唯一性
     * @param skGoodsSeckill
     */
    public void addToken(SkGoodsSeckill skGoodsSeckill) {
        redisTemplate.opsForValue().set(RedisConstants.GOODS_STOCK + skGoodsSeckill.getId() + "_" +skGoodsSeckill.getGoodsId(),
                skGoodsSeckill.getStockCount().toString(), 60, TimeUnit.HOURS);
    }
    public boolean hasStock(Long seckillId,Long goodsId){
        String key = RedisConstants.GOODS_STOCK + seckillId + "_" + goodsId;
        String goodsStock = redisTemplate.opsForValue().get(key);
        Integer stock = Integer.valueOf(goodsStock);
        if (stock <= 0){
            //已经没有库存，删除id
            redisTemplate.delete(key);
            return false;
        }
        return true;
    }
}
